package com.swss.g.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.swss.g.base.impl.BaseServiceImpl;
import com.swss.g.user.bean.Module;
import com.swss.g.user.bean.Role;
import com.swss.g.user.bean.RolePopedom;
import com.swss.g.user.dao.ModuleDAO;
import com.swss.g.user.dao.PopedomDAO;
import com.swss.g.user.dao.RoleDAO;
import com.swss.g.user.dao.RolePopedomDAO;
import com.swss.g.user.service.RoleService;
import com.swss.util.Pagination;

public class RoleServiceImpl extends BaseServiceImpl<Role> implements
		RoleService {
	private RoleDAO roleDao;

	private ModuleDAO moduleDao;

	private PopedomDAO popedomDao;

	private RolePopedomDAO RolePopedomDao;

	public RoleDAO getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDAO roleDao) {
		this.setBaseDao(roleDao);
		this.roleDao = roleDao;
	}

	public ModuleDAO getModuleDao() {
		return moduleDao;
	}

	public void setModuleDao(ModuleDAO moduleDao) {
		this.moduleDao = moduleDao;
	}

	public PopedomDAO getPopedomDao() {
		return popedomDao;
	}

	public void setPopedomDao(PopedomDAO popedomDao) {
		this.popedomDao = popedomDao;
	}

	@SuppressWarnings("unchecked")
	public List<Module> findAllPopedom(Pagination pagination) throws Exception {
		// TODO Auto-generated method stub
		return this.moduleDao.findAll(pagination);
	}

	public RolePopedomDAO getRolePopedomDao() {
		return RolePopedomDao;
	}

	public void setRolePopedomDao(RolePopedomDAO rolePopedomDao) {
		RolePopedomDao = rolePopedomDao;
	}

	public void savePopes(List<RolePopedom> RolePopedoms) throws Exception {
		// TODO Auto-generated method stub

		this.RolePopedomDao.saveOrUpdateAll(RolePopedoms);
	}

	public List<RolePopedom> findRolePopedomByExample(RolePopedom rolePopedom,
			Pagination pagination) throws Exception {
		// TODO Auto-generated method stub

		return this.RolePopedomDao.findByExample(rolePopedom, pagination);
	}

	/**
	 * 更新角色的相关关系
	 */
	@SuppressWarnings("unchecked")
	public void updateRolePopedom(Integer roleId, List<RolePopedom> rolePopedoms)
			throws Exception {
		// TODO Auto-generated method stub

		Pagination p = new Pagination();
		p.setUsepage(false);

		RolePopedom rp = new RolePopedom();
		rp.setRoleId(roleId);

		List<RolePopedom> oldrps = this.RolePopedomDao.findByExample(rp, p);
		// add
		List<RolePopedom> adds = new ArrayList<RolePopedom>();

		if (oldrps.size() > 0) {

			// del
			List<RolePopedom> dels = new ArrayList<RolePopedom>();

			for (RolePopedom oldrp : oldrps) {
				for (RolePopedom newrp : rolePopedoms) {
					// 要删除的权限（数据库中存在，传值中不存在的权限）
					if (!oldrp.getPopedomId().equals(newrp.getPopedomId())) {
						dels.add(oldrp);
					}
				}
			}

			for (RolePopedom newrp : rolePopedoms) {
				for (RolePopedom oldrp : oldrps) {
					// 要新增的权限（数据库中不存在，传值中存在的权限）
					if (!newrp.getPopedomId().equals(oldrp.getPopedomId())) {
						adds.add(newrp);
					}
				}
			}

			this.RolePopedomDao.deleteAll(dels);

		} else {
			adds = rolePopedoms;
		}

		for (RolePopedom add : adds) {
			this.RolePopedomDao.save(add);
		}

	}

}
